package com.glut.controller;

import com.glut.service.ErrorQuestionService;
import com.glut.service.ExaminationService;
import com.glut.service.SstsFeedbackService;
import com.glut.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:沈林玉
 * @date:2020/3/13 17:31
 * @Description:
 */
@Controller
@RequestMapping("error")
public class ErrorQuestionController {

    @Autowired
    ErrorQuestionService errorQuestionService;

    @Autowired
    ExaminationService examinationService;

    /**
     * 1.查询错题本信息
     * @param
     * @param session
     * @return
     */
    @RequestMapping("/selectErrorQByUserId/{sstsUserID}")
    public String selectErrorQByUserId(@PathVariable String sstsUserID,
                                        HttpSession session){

        SstsUsers sstsUsers = new SstsUsers();
        sstsUsers.setSstsUserID(sstsUserID);
        List<ErrorQuestion> errorQuestionList = errorQuestionService.selectAllErrorQ();
        List<SdAnswers> sdErrorByIdList = new ArrayList<>();
        List<FsAnswers> fsErrorByIdList = new ArrayList<>();
        // 查看详情试题
        for (ErrorQuestion errorQuestion:errorQuestionList){
            List<SdAnswers> sdErrorById = examinationService.selectSdErrorById(errorQuestion);
            List<FsAnswers> fsErrorById = examinationService.selectFsErrorById(errorQuestion);
            List<FsAnswers> shErrorById = examinationService.selectShErrorById(errorQuestion);
            sdErrorByIdList.addAll(sdErrorById);
            fsErrorByIdList.addAll(fsErrorById);
            fsErrorByIdList.addAll(shErrorById);
        }

        session.setAttribute("sdErrorByIdList",sdErrorByIdList);
        session.setAttribute("fsErrorByIdList",fsErrorByIdList);

        session.setAttribute("errorQuestionList",errorQuestionList);
        return "redirect:/views/exam/errorQuestion.jsp";
    }

    /**
     * 2.插入错题信息
     * @param
     * @return
     */
    @RequestMapping(value = "/insertErrorQuestion",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Integer> insertErrorQuestion(String examNo,
                                                   String qId,
                                                   HttpSession session){
        SstsUsers loginUser = (SstsUsers)session.getAttribute("loginUser");
        ErrorQuestion errorQuestion = new ErrorQuestion(examNo, qId, loginUser.getSstsUserID());
        // 查询是否已经收藏
        ErrorQuestion selectErrorQById = errorQuestionService.selectErrorQById(errorQuestion);

        Integer insertErrorQuestion = null;

        // 未收藏
        if (selectErrorQById==null){
            // 添加至收藏夹
            insertErrorQuestion = errorQuestionService.insertErrorQuestion(errorQuestion);
        }else {
            // 已经收藏就返回2
            insertErrorQuestion=2;
        }
        Map<String,Integer> map = new HashMap<>();
        map.put("insertErrorQuestion",insertErrorQuestion);
        return map;
    }

    /**
     * 2.删除错题信息
     * @param
     * @return
     */
    @RequestMapping(value = "/deleteErrorQuestion",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Integer>  deleteErrorQuestion(String examNo,
                                       String qId,
                                      HttpSession session){
        SstsUsers loginUser = (SstsUsers)session.getAttribute("loginUser");

        ErrorQuestion errorQuestion = new ErrorQuestion(examNo, qId, loginUser.getSstsUserID());

        // 取消收藏
        Integer deleteErrorQuestion = errorQuestionService.deleteErrorQuestion(errorQuestion);

        Map<String,Integer> map = new HashMap<>();
        map.put("deleteErrorQuestion",deleteErrorQuestion);
        return map;
    }

}
